package com.dldata.devtools30.controller;

import com.dldata.devtools30.common.EnumUtils;
import com.dldata.devtools30.entity.DevtFunctionListEntity;
import com.dldata.devtools30.repository.api.DevtFunctionListEntityDao;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.*;

/**

 */
@Controller
@RequestMapping(value = "/DevFunctionListController")
public class DevFunctionListController extends DevBase30Controller {
    
    @Autowired
    DevtFunctionListEntityDao devtFunctionListEntityDao;
    
    @RequestMapping(value="toList")
    public String toList(HttpServletRequest request,
                        HttpServletResponse response,
                        ModelMap model
    ) {
        return "thymeleaf/devtools30/index/v_devfunctionlist";
    }
    
    @RequestMapping(value="toEdit")
    public String toEdit(HttpServletRequest request,
                         HttpServletResponse response,
                         ModelMap model
    ) {
        DevtFunctionListEntity devtFunctionListEntity=new DevtFunctionListEntity();
        if(!StringUtil.isEmptyOrLength0(request.getParameter("id"))){
            devtFunctionListEntity=commonApiDao.find(DevtFunctionListEntity.class,request.getParameter("id"));
        }
        model.addAttribute("entity",devtFunctionListEntity);
    
        EnumUtils.FunctionTypeEnum[] functionTypeEnums = EnumUtils.FunctionTypeEnum.values();
        ArrayList arrayList=new ArrayList();
        for (int i = 0; i < functionTypeEnums.length; i++) {
            EnumUtils.FunctionTypeEnum functionTypeEnum = functionTypeEnums[i];
            HashMap hashMap=new HashMap();
            hashMap.put("value",functionTypeEnum.name());
            arrayList.add(hashMap);
        }
        model.addAttribute("typeList",arrayList);
        return "thymeleaf/devtools30/index/v_devfunctionEdit";
    }
    
    
    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(DevtFunctionListEntity entity){
        if(StringUtil.isEmptyOrLength0(entity.getName())){
            return Result.error("名称不能为空");
        }
        if(StringUtil.isEmptyOrLength0(entity.getUrl())){
            return Result.error("路径URL不能为空");
        }
        
        if(StringUtil.isEmptyOrLength0(entity.getId())){
            entity.setId(UUID.randomUUID().toString());
        }
        if(StringUtil.isEmptyOrLength0(entity.getCreatetime())){
            entity.setCreatetime(new Timestamp(System.currentTimeMillis()));
        }
        entity.setDelflag(0);
        commonApiDao.save(entity);
        
        return Result.ok();
    }
    
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(String id){
    
        DevtFunctionListEntity functionListEntity = commonApiDao.findone(DevtFunctionListEntity.class, id);
        if(functionListEntity!=null&&Integer.valueOf("1").equals(functionListEntity.getDelflag())){
            commonApiDao.deleteObj(functionListEntity);
            return Result.ok("删除成功.");
        }
        functionListEntity.setDelflag(1);
        commonApiDao.save(functionListEntity);
        return Result.ok("已废除");
    }
    
    @RequestMapping("/getlist")
    @ResponseBody
    public Result getlist(String deleteflag,String search){
//        List<DevtFunctionListEntity> listEntityList =commonApiDao.getScrollData(DevtFunctionListEntity.class).getResultlist();
        search=StringUtil.isEmptyOrLength0(search)?"%%":("%"+search+"%");
        if(StringUtil.isEmptyOrLength0(deleteflag)){
            return Result.ok(devtFunctionListEntityDao.getFunctionList(search));
        }else{
            return Result.ok(devtFunctionListEntityDao.getFunctionList(search,Integer.valueOf(deleteflag)));
        }
    }
}
